package cn.cl.gulimall.product.controller;

import java.util.List;
import java.util.Map;

import cn.cl.gulimall.product.entity.AttrEntity;
import cn.cl.gulimall.product.service.AttrAttrgroupRelationService;
import cn.cl.gulimall.product.service.AttrService;
import cn.cl.gulimall.product.service.CategoryService;
import cn.cl.gulimall.product.vo.AttrGroupRelationVo;
import cn.cl.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.cl.gulimall.product.entity.AttrGroupEntity;
import cn.cl.gulimall.product.service.AttrGroupService;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.R;


/**
 * 属性分组
 *
 * @author 高志红
 * @email 1739745826@qq.com
 * @date 2020-05-07 19:59:24
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
	@Autowired
	private AttrGroupService attrGroupService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private AttrService attrService;

	/**
	 * 根据分组ID找到改分组关联的所有属性
	 */
	@GetMapping("/{attrgroupId}/attr/relation")
	public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
		List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
		return R.ok().put("data", entities);
	}

	/**
	 * 列表
	 */
	@GetMapping("/list/{catelogId}")
	public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
		PageUtils page = attrGroupService.queryPage(params, catelogId);
		return R.ok().put("page", page);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrGroupId}")
	public R info(@PathVariable("attrGroupId") Long attrGroupId) {
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		// 找到catelogId的完整路径
		Long[] path = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
		return R.ok().put("attrGroup", attrGroup);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody AttrGroupEntity attrGroup) {
		attrGroupService.save(attrGroup);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	// @RequiresPermissions("product:attrgroup:update")
	public R update(@RequestBody AttrGroupEntity attrGroup) {
		attrGroupService.updateById(attrGroup);
		return R.ok();
	}

	/**
	 * 删除属性分组和属性的关联关系
	 */
	@PostMapping("/attr/relation/delete")
	public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
		attrService.deleteRelation(vos);
		return R.ok();
	}

	/**
	 * 获取当前属性分组没有关联的属性
	 */
	@GetMapping("/{attrgroupId}/noattr/relation")
	public R attrNoRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId) {
		PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
		return R.ok().put("page", page);
	}


	@Autowired
	AttrAttrgroupRelationService attrAttrgroupRelationService;

	/**
	 * 保存关联关系
	 */
	@PostMapping("/attr/relation")
	public R addRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {
		attrAttrgroupRelationService.saveBatch(attrGroupRelationVos);
		return R.ok();
	}

	/**
	 * 获取分类下所有的分组以及属性
	 */
	@GetMapping("/{catelogId}/withattr")
	public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
		// 1、查询当前分类下的所有属性分组
		// 2、查出每个属性分组的所有属性
		List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
		return R.ok().put("data", vos);
	}
}
